package bjfuwjx.exp2;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import java.io.*;
import java.util.*;

class Text{
    String id;
    String txt1;
    String txt2;
    String txt3;
    String txt4;
    String txt5;
    String txt;
    String key_txt;
    int length;
    int key_num;
    int key_len;
    double rate;

    public void set_id(String m_id)
    {
        id=m_id;
    }
    public String get_id()
    {
        return id;
    }
    public void set_txt(String m_txt)
    {
        txt=m_txt;
    }
    public String get_txt() {return txt;}
    public void set_txt1(String m_txt)
    {
        txt1=m_txt;
    }
    public String get_txt1() {return txt1;}
    public void set_txt2(String m_txt)
    {
        txt2=m_txt;
    }
    public String get_txt2() {return txt2;}
    public void set_txt3(String m_txt)
    {
        txt3=m_txt;
    }
    public String get_txt3() {return txt3;}
    public void set_txt4(String m_txt)
    {
        txt4=m_txt;
    }
    public String get_txt4() {return txt4;}
    public void set_txt5(String m_txt)
    {
        txt5=m_txt;
    }
    public String get_txt5() {return txt5;}
    public void set_key_txt(String m_key_txt)
    {
        key_txt=m_key_txt;
    }
    public String get_key_txt() {return key_txt;}
    public int get_length(String s)
    {
        for (int i=0;i<s.length();i++) {
            if (s.charAt(i)!='\n')
                length++;
        }
        return length;
    }
    public void setKey_len(int m_keylength)
    {
        key_len=m_keylength;
    }
    public int getKey_len()
    {
        return key_len;
    }
    public void setKey_num(int m_keynum)
    {
        key_num=m_keynum;
    }
    public int getKey_num()
    {
        return key_num;
    }
    public void setRate(int m_keylength,int m_txtlength) {
        rate=1.0*m_keylength/m_txtlength;
    }
    public double getRate()
    {
        return rate;
    }
}

public class TermFrequency {
    public static HashSet<String> s = new HashSet<>();
    public static List<Text> l = new ArrayList<>();
    public static List<Text> a = new ArrayList<>(l);
    public static List<Text> b = new ArrayList<>(l);

    public static void readTxtFile() {
        try {
            String encoding="UTF-8";
            File file=new File("src\\bjfuwjx\\exp2\\Keywords.txt");
            if (file.isFile()&&file.exists()) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null){
                    s.add(lineTxt);
                }
                read.close();
            }
            else {
                System.out.println("找不到指定的文件");
            }
        }
        catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
    }

    public static void writeExcel() throws IOException, RowsExceededException, WriteException {
        File file = new File("Result.xls");
        try {
            file.createNewFile();
        }
        catch (IOException e) {
            System.out.println("文件创建失败");
            e.printStackTrace();
        }
        WritableWorkbook workbook=Workbook.createWorkbook(file);
        WritableSheet sheet=workbook.createSheet("词频统计表", 0);
        String[] titles={"股票代号","文本长度","关键词个数","关键词长度","关键词密度","关键词列表"};
        Label label=null;
        for (int i=0;i<titles.length;i++) {
            label=new Label(i,0,titles[i]);
            sheet.addCell(label);
        }
        getall();
        label=null;
        for (int i=0;i<a.size();i++) {
            label=new Label(0,i+1,a.get(i).id);
            sheet.addCell(label);
        }
        label=null;
        for (int i=0;i<a.size();i++) {
            label=new Label(1,i+1,a.get(i).length+"");
            sheet.addCell(label);
        }
        func();
        label=null;
        for (int i=0;i<a.size();i++) {
            label=new Label(2,i+1,b.get(i).key_num+"");
            sheet.addCell(label);
        }
        label=null;
        for (int i=0;i<a.size();i++) {
            label=new Label(3,i+1,b.get(i).key_len+"");
            sheet.addCell(label);
        }
        label=null;
        for (int i=0;i<a.size();i++) {
            label=new Label(4,i+1,b.get(i).rate+"");
            sheet.addCell(label);
        }
        label=null;
        for (int i=0;i<a.size();i++) {
            label=new Label(5,i+1,b.get(i).key_txt+"");
            sheet.addCell(label);
        }

        workbook.write();
        workbook.close();
    }
    public static void readExcel() {
        Workbook workbook = null;
        try {
            workbook = Workbook.getWorkbook(new File("src\\bjfuwjx\\exp2\\SourceText.xls"));
        }
        catch (IOException e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        catch (BiffException e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        Sheet sheet = workbook.getSheet(0);
        for (int i=1;i<sheet.getRows();i++) {
            Cell id_cell = sheet.getCell(0, i);
            Cell txt1_cell = sheet.getCell(0, i);
            Cell txt2_cell = sheet.getCell(1, i);
            Cell txt3_cell = sheet.getCell(2, i);
            Cell txt4_cell = sheet.getCell(3, i);
            Cell txt5_cell = sheet.getCell(4, i);
            Text aa = new Text();
            aa.set_id(id_cell.getContents());
            aa.set_txt1(txt1_cell.getContents());
            aa.set_txt2(txt2_cell.getContents());
            aa.set_txt3(txt3_cell.getContents());
            aa.set_txt4(txt4_cell.getContents());
            aa.set_txt5(txt5_cell.getContents());
            l.add(aa);
        }
        Collections.sort(l,new Comparator<Text>(){
            @Override
            public int compare(Text o1, Text o2) {
                if (o1.id.compareTo(o2.id)>0)
                    return 1;
                else if (o1.id.compareTo(o2.id)==0)
                    return 0;
                return -1;
            }
        });
        workbook.close();
    }

    public static List<String> remove(List<String> list, String target) {
        for (String item : list) {
            if (item.equals(target)) {
                list.remove(item);
                break;
            }
        }
        return list;
    }

    public static void getall() {
        String id=l.get(0).id;
        String txt=l.get(0).txt3+l.get(0).txt4+l.get(0).txt5;
        for (int i=1;i<l.size();i++) {
            Text tmp=new Text();
            if (l.get(i).id!=l.get(i-1).id) {
                tmp.set_id(l.get(i-1).id);
                tmp.set_txt(txt);
                tmp.get_length(txt);
                a.add(tmp);
                txt=l.get(i).txt3+l.get(i).txt4+l.get(i).txt5;
            }
            else {
                txt+=l.get(i).txt3+l.get(i).txt4+l.get(i).txt5;
            }
            if (i==l.size()-1) {
                tmp.set_id(l.get(l.size()-1).id);
                tmp.set_txt(txt);
                tmp.get_length(txt);
                a.add(tmp);
            }
        }
    }

    public static void func() {
        for (int i=0;i<a.size();i++) {
            List<String> str = new ArrayList<>();
            int n=0;
            String t=a.get(i).txt;
            int idx=0;
            for (String p:s) {
                idx=t.indexOf(p);
                while (idx!=-1) {
                    n++;
                    str.add(p);
                    idx=t.indexOf(p,idx+1);
                }
            }
            String ss="";
            String[] st=str.toArray(new String[]{});
            //System.out.println(Arrays.toString(st));
            for (int j=0;j<st.length;j++) {
                if (st[j].compareTo("不确定性")==0)
                    remove(str,"不确定");
                else if (st[j].compareTo("经济下行")==0)
                    remove(str,"下行");
                else if (st[j].compareTo("产能过剩")==0)
                    remove(str,"过剩");
                else if (st[j].compareTo("增速放缓")==0)
                    remove(str,"放缓");
                else if (st[j].compareTo("日趋激烈")==0)
                    remove(str,"激烈");
                else if (st[j].compareTo("价格波动")==0||st[j].compareTo("汇率波动")==0)
                    remove(str,"波动");
                else if (st[j].compareTo("信用风险")==0||st[j].compareTo("市场风险")==0||st[j].compareTo("流动性风险")==0||st[j].compareTo("经营风险")==0||st[j].compareTo("风险管理")==0)
                    remove(str,"风险");
            }
            int k=str.size();
            if (k==0)
                ss+="#";
            else {
                ss+=str.get(0);
                for (int j=1;j<k;j++)
                    ss+=","+str.get(j);
            }
            int len=0;
            for (int j=0;j<ss.length();j++) {
                if (ss.charAt(j)!=','&&ss.charAt(j)!='#'&&ss.charAt(j)!=' ')
                    len++;
            }
            Text tt=new Text();
            tt.setKey_num(str.size());
            tt.setKey_len(len);
            tt.setRate(len,a.get(i).length);
            tt.set_key_txt(ss);
            b.add(tt);
            //System.out.println(a.get(i).id+" "+tt.getKey_len());
        }
    }

    public static void printL(List c) {
        Iterator<List> it=c.iterator();
        int i=1;
        while (it.hasNext()) {
            System.out.println(it);
        }
    }

    public static void main(String[] args) throws WriteException, IOException {
        readTxtFile();
        readExcel();
        writeExcel();
        System.out.println("词频统计结束");
    }
}